﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowledgeAssistant
{
    public partial class TestResultsForm : Form
    {
        private int archive_test_id = 0;
        private DataTable testinfo;
        private DataTable questionsinfo;
        private DataTable answersinfo;

        public TestResultsForm(int archive_test_id)
        {
            InitializeComponent();
            this.archive_test_id = archive_test_id;
            this.testinfo = DatabaseConnect.execSP("GetTestResults", 
                new List<string>() {"0","0",archive_test_id.ToString() 
                }).Tables[0];
            DataSet tables = TestUtil.GetTestResultsInfo(this.archive_test_id);
           
            this.questionsinfo = tables.Tables[0];
            this.answersinfo = tables.Tables[1];
            InitTestFields();
            InitQuestionsAnswersTable();
            
        }

        private void InitTestFields()
        {
            marktextBox6.Text = testinfo.Rows[0]["mark"].ToString();
            themenametextBox2.Text = testinfo.Rows[0]["theme_name"].ToString();
            subjectnametextBox3.Text = testinfo.Rows[0]["subject_name"].ToString();
            usernametextBox4.Text = testinfo.Rows[0]["fio"].ToString();
            durationtextBox5.Text = testinfo.Rows[0]["duration_time"].ToString();
            startdatetextBox7.Text = testinfo.Rows[0]["date_begin"].ToString();
        }

        private void InitQuestionsAnswersTable()
        {
            bool line = false;
            foreach (DataRow row in questionsinfo.Rows)
            {
                if (!line && int.Parse(row["question_type"].ToString()) != 1)
                {
                    DataGridViewRow separate_line = new DataGridViewRow();
                    separate_line.Height = 10;
                    separate_line.DefaultCellStyle.BackColor = Color.DarkGray;
                    line = true;
                    dataGridView1.Rows.Add(separate_line);
                }
                
                DataGridViewRow question = new DataGridViewRow();
                question.CreateCells(dataGridView1);
                question.Cells[0].Value = "─"; 
                question.Cells[1].Value = row["description"].ToString();
                question.Cells[2].Value = row["question_type_desc"].ToString();
                question.Cells[3].Value = row["levelname"].ToString();
                question.Cells[4].Value = ((row["true"].ToString() != "0")?"0":row["level_value"].ToString()) +" баллов" ;
                question.Height = 28;
                question.DefaultCellStyle.BackColor = Color.LightGray;
                
                dataGridView1.Rows.Add(question);

                foreach (DataRow rowa in answersinfo.Select("archive_question_id = " + row["archive_question_id"], "archive_answer_id"))
                {
                    DataGridViewRow answer = new DataGridViewRow();
                    answer.CreateCells(dataGridView1);
                    string ansresult = "";
                    string answersym = "";
                    Color color = Color.White;

                    if((bool)rowa["checked"])
                    {
                        answersym = "▪";
                        if ((bool)rowa["true"])
                        {
                            color = Color.FromArgb(192, 255, 192);
                            ansresult = "Верно";
                        }
                        else
                        {
                            color = Color.FromArgb(255, 192, 192);
                            ansresult = "Неверно";
                        }
                    }

                    answer.Cells[0].Value = answersym;//(aindex++).ToString();
                    answer.Cells[1].Value = rowa["description"].ToString();
                    answer.Cells[2].Value = ansresult;
                    answer.Cells[3].Value = "";
                    answer.DefaultCellStyle.BackColor = color;
                    dataGridView1.Rows.Add(answer);
                }
            }
        }        

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "─")
            {
                for (int i = e.RowIndex + 1; i < dataGridView1.Rows.Count; i++)
                {
                    string value = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    if (value != "+" && value != "─")
                    {
                        dataGridView1.Rows[i].Visible = false;
                    }
                    else { break; }
                }

                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "+";
            }
            else if (e.ColumnIndex == 0 && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "+")
            {
                for (int i = e.RowIndex + 1; i < dataGridView1.Rows.Count; i++)
                {
                    string value = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    if (value != "+" && value != "─")
                    {
                        dataGridView1.Rows[i].Visible = true;
                    }
                    else { break; }
                }
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "─";
            }
        }

        private void TestResultsForm_Load(object sender, EventArgs e)
        {

        }
    }
}
